﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBlob : Enemy
{

	public int cntJump;     // 单方向最大跳跃次数

	int cnt;
	bool isLeft = true;


	protected override void Start() {
		base.Start();
		SetIniAttributes(5, 5, 2, EnemyType.EnemyBlob);           // 血量:5  价值:5  攻击力:2
		cnt = cntJump;
		Debug.Log(Type);
	}

	public override void OnPauseAni() {
		base.OnPauseAni();

	}

	public override void OnContinueAni() {
		base.OnContinueAni();

	}


	public override void OnEnterAniEvent(EnemyState state) {
		if (state == EnemyState.Idle) {
			Vector3 scale = transform.localScale;
			if (cnt == 0) transform.localScale = new Vector3(-scale.x, scale.y, scale.z);

		} else if (state == EnemyState.Walk) {
			if (cnt == 0) isLeft = !isLeft;
			cnt = (cnt + 1) % cntJump;
			StartCoroutine(WalkDistance());
		}
	}

	public override void OnExitAniEvent(EnemyState state) {
		if (state == EnemyState.Idle) {
			SetEnemyState(EnemyState.Walk, true);
		} else if (state == EnemyState.Walk) {
			SetEnemyState(EnemyState.Walk, false);
		}
	}


	private IEnumerator WalkDistance() {
		int loop = 30;
		rig.AddForce(Vector3.up * 120);
		int dir = isLeft ? 1 : -1;

		while (loop > 0) {
			transform.Translate(Vector3.left * Time.deltaTime * dir * 4);
			loop--;
			yield return null;
		}
	}

	public override void OnEnterEnemyDieEvent() {
		base.OnEnterEnemyDieEvent();
		Destroy(rig);
	}




}
